<?php
/*
 * Copyright (c) 2021.  Liuhongbin All rights reserved.
 * @domain http://dkyx.cc
 * @author liuhongbin <514660094@qq.com>
 */

namespace app\common\model\visit;

use app\common\model\BaseModel;
use app\common\model\platform\Platform;
use think\facade\Cache;

class Visit extends BaseModel
{
    protected $autoWriteTimestamp = false;

    /**
     * @var VisitIp
     */
    private VisitIp $ipModel;

    public function __construct(array $data = [])
    {
        parent::__construct($data);
        $this->ipModel = new VisitIp();
    }

    public function statDateRange($start, $end)
    {
        $result = array();
        if (empty($start) || empty($end)) {
            return $result;
        }
        $start = strtotime($start);
        $end = strtotime($end);
        $i = 0;
        while (strtotime(end($result)) < $end) {
            $result[] = date('Ymd', $start + $i * 86400);
            $i++;
        }
        return $result;
    }

    public function getVisitListForDashboard()
    {
        $data_x = $this->statDateRange(date('Ymd', strtotime('-6 days')), date('Ymd'));
        $data_y = $this->whereIn('date', $data_x)->select()->toArray();
        $data_y_date = [];
        foreach ($data_y as $key => $value) {
            if (!empty($data_y_date[$value['date']][$value['type']])) {
                $data_y_date[$value['date']][$value['type']]['count'] = $data_y_date[$value['date']][$value['type']]['count'] + $value['count'];
            } else {
                $data_y_date[$value['date']][$value['type']] = [
                    'count' => $value['count']
                ];
            }
        }
        $series = [];

        $types = array('admin', 'platform', 'addon', 'customer');
        foreach ($types as $key => $value) {
            foreach ($data_x as $k => $date) {
                if (!empty($data_y_date[$date][$value])) {
                    $series[$value][] = $data_y_date[$date][$value]["count"];
                } else {
                    $series[$value][] = 0;
                }
            }
        }
        return ['data_x' => $data_x, 'series' => $series];
    }

    /**
     * 更新今日访问次数
     * @param string $type
     * @param string $name
     * @return bool
     */
    public function updateVisitToday($type = '', $name = '')
    {
        $types = array('admin', 'platform', 'customer', 'api', 'index', 'addon');
        $type = input('visit_type', $type);
        $plaid = get_plaid();
        if (empty($type) || !in_array($type, $types) || ($type == 'addon' && empty($name)) || ($type == 'platform' && !$plaid) || $type == false) {
            return false;
        }
        if ($type == 'admin' && $plaid) {
            $plaid = 0;
        }
        $key = "update_visit_" . $plaid . "_" . $type . "_" . $name;
        $count = Cache::get($key);
        if ($count > 20) {
            if ($plaid) {
                $pla_type = Platform::where(['plaid' => $plaid])->cache()->value('type') ?? -1;
            } else {
                $pla_type = -1;
            }
            $today = date('Ymd');
            $visit_res = $this->where(['date' => $today, 'name' => $name, 'type' => $type, 'plaid' => $plaid])->lock(true)->findOrEmpty();
            if ($visit_res->isEmpty()) {
                $visit_data = array(
                    'date' => $today, 'name' => $name, 'type' => $type, 'plaid' => $plaid, 'count' => 1, 'pla_type' => $pla_type
                );
                $visit_res->lock(true)->save($visit_data);
            } else {
                $visit_res->count = $visit_res->count + $count;
            }
            $visit_res->save();
            Cache::set($key, 0);
        } else {
            Cache::set($key, $count + 1);
        }
        return true;
    }
}
